package com.framework.bdf4j.sysadm.controller;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.bdf4j.annotations.SysLog;
import com.framework.bdf4j.comm.Constants;
import com.framework.bdf4j.comm.persist.PageDomain;
import com.framework.bdf4j.comm.util.R;
import com.framework.bdf4j.comm.util.StringUtils;
import com.framework.bdf4j.sysadm.entity.SysTenant;
import com.framework.bdf4j.sysadm.service.SysTenantService;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;

/**
 * 租户管理
 *
 */
@Controller
@AllArgsConstructor
@RequestMapping("/admin/tenant")
@Api(value = "tenant", tags = "租户管理")
public class SysTenantController {

    private final SysTenantService sysTenantService;

    // 跳转到角色列表页
    @GetMapping("/index")
    public String goRole() {
        return "sysadm/tenant/tenantList";
    }

    // 跳转到租户编辑页
    @GetMapping("/goEditTenant")
    public String goEditTenant(@RequestParam(required = false) String id, @RequestParam String oper, Model model) {
        SysTenant systenant = new SysTenant();
        if (!"add".equals(oper)) {
            systenant = sysTenantService.getById(Integer.parseInt(id));
        }
        model.addAttribute("viewobject", systenant);
        model.addAttribute("oper", oper);
        return "sysadm/tenant/tenantEdit";
    }

    /**
     * 分页查询租信息
     *
     * @param page
     *            分页对象
     * @return 分页对象
     */
    @GetMapping("/page")
    @ResponseBody
    public R getTenantPage(Page page, PageDomain domain, SysTenant systenant) {
        OrderItem orderitem = StringUtils.getOrderItem(domain);
        if (orderitem != null) {
            page.addOrder(orderitem);
        }
        return R.ok(sysTenantService.page(page, Wrappers.query(systenant)));
    }

    /**
     * 新增租户
     *
     * @param sysTenant
     *            租户
     * @return R
     */
    @SysLog("新增租户")
    @PostMapping
    @ResponseBody
    @PreAuthorize("@pms.hasPermission('admin_systenant_add')")
    @CacheEvict(value = Constants.TENANT_DETAILS, allEntries = true)
    public R save(SysTenant sysTenant) {
        return R.ok(sysTenantService.saveTenant(sysTenant));
    }

    /**
     * 修改租户
     *
     * @param sysTenant
     *            租户
     * @return R
     */
    @SysLog("修改租户")
    @PutMapping
    @ResponseBody
    @PreAuthorize("@pms.hasPermission('admin_systenant_edit')")
    @CacheEvict(value = Constants.TENANT_DETAILS, allEntries = true)
    public R updateById(SysTenant sysTenant) {
        return R.ok(sysTenantService.updateById(sysTenant));
    }

    /**
     * 通过id删除租户
     *
     * @param id
     *            id
     * @return R
     */
    @SysLog("删除租户")
    @DeleteMapping("/{id}")
    @ResponseBody
    @PreAuthorize("@pms.hasPermission('admin_systenant_del')")
    @CacheEvict(value = Constants.TENANT_DETAILS, allEntries = true)
    public R removeById(@PathVariable Integer id) {
        return R.ok(sysTenantService.removeById(id));
    }

    /**
     * 查询全部有效的租户
     *
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public R list() {
        List<SysTenant> tenants =
            sysTenantService.getNormalTenant().stream().filter(tenant -> tenant.getStartTime().before(new Date()))
                .filter(tenant -> tenant.getEndTime().after(new Date())).collect(Collectors.toList());
        return R.ok(tenants);
    }

}
